/**
 * @file    AddCategoryCTRL
 * @author  Haseeb Yousaf
 * @see     LICENSE (MIT style license file)
 * @version 1.0
 * @date    Nov 15, 2011 8:15:37 PM
 */
package carrello.impl.controller.administrator;

import carrello.exception.CarrelloException;
import carrello.model.entitydao.CategoryDAO;
import carrello.impl.controller.AbstractCTRL;
import carrello.model.association.AssociationFactory;
import java.util.HashSet;
import carrello.model.association.CategoryHasAttributes;
import java.util.Set;
import java.util.List;
import carrello.model.entities.Attribute;
import carrello.model.entities.Category;
import carrello.model.entities.EntityFactory;
import org.apache.log4j.Logger;

public class AddCategoryCTRL extends AbstractCTRL{
        Logger log = Logger.getLogger(AddCategoryCTRL.class);
        private static Set<Attribute> attributes ;
        
        public AddCategoryCTRL() {}
        public void addCategory(String pname,String name, String description, List<String[]> attribute, String[] inherit) throws CarrelloException
        {
            init();
            attributes = new HashSet<Attribute>();
            for (int i = 0; i < attribute.size(); i++) {
                try {
                    attributes.add(this.getAttribute(attribute.get(i)));
                } catch (CarrelloException cex) {
                    throw cex;
                } // try
            }
           
            CategoryDAO dao = new CategoryDAO();
            Category parent = dao.getByName(pname);
            if (inherit.length == 2) {
                if (!(parent.getAttribute().isEmpty())) {
                    attributes.addAll(parent.getAttribute());
                } // else
            } // else
            Category cat = EntityFactory.createCategory(name, description);
            
            cat.setParentId(parent);
            if (parent.getchildren() != null){
                parent.getchildren().add(cat);
            }
            dao.create(parent);
            if (!(attributes.isEmpty())) {
            CategoryHasAttributes categoryHasAttributes = AssociationFactory.createCategoryHasAttributes(cat, attributes);
            categoryHasAttributes.save();
            }
            end();
            
        }
        public void addCategory(String pname, String name, String description, String[] inherit)
        {
            init();
            CategoryDAO dao = new CategoryDAO();
            Category parent = dao.getByName(pname);
            Category cat = EntityFactory.createCategory(name, description);
            
            cat.setParentId(parent);
            if (parent.getchildren() != null){
                parent.getchildren().add(cat);
            }
            if (inherit.length == 2) {
                if (!(parent.getAttribute().isEmpty())) {
                    attributes = new HashSet<Attribute>();
                    attributes.addAll(parent.getAttribute());
                    dao.create(parent);
                    CategoryHasAttributes categoryHasAttributes = AssociationFactory.createCategoryHasAttributes(cat, attributes);
                    categoryHasAttributes.save();
                } else {
                    dao.create(parent);
                    dao.create(cat);
                } // if
            } else {
                dao.create(parent);
                dao.create(cat);
            } // if
            end();
        }
        
        private Attribute getAttribute(String[] tuple) throws CarrelloException
        {
            Attribute att = null;
            Set<Attribute> attSet = new HashSet<Attribute>();
            String name     = tuple[0];
            String type     = tuple[1];
            String required = tuple[2];
            char require = (required.equals("checked")) ? '1' : '0';
            // check whether attribute exist
            if (name.trim().equals("")) {
                log.info("Attribute Name is empty");
                throw new CarrelloException("Attribute Name is empty : please supply a valid name");
            } // if
            att = Attribute.getAttributeByName(name);
            if (att == null) {
                att = EntityFactory.createAttribute(name, type, require);
                return (att);
            } else {
                return att;
            } // if
        } // getAttribute
                
//    public static void main (String [] args)
//    {
//        getEntityManager().beginUnitOfWork();
//         Category parent = new CategoryDAO().getByName("Sci-Fi");
////         Category c = new Category("ficiton", "Sci-Fi books",'0');
//         
//         addAttribute("name","String", '1');
//         addAttribute("isbn","Integer", '1');
//         save();getEntityManager().endUnitOfWork();
//    }
}
